$(function () {

    //工具栏搜索框快速查询
    $("#competitor-search").searchbox({
        searcher : function () {
            //获取快速搜索的类型
            var searchName = $('#competitor-search').searchbox('getName');
            //获取搜索框的值
            var searchVal = $('#competitor-search').searchbox('getValue');
            if (searchName == 'comName') {
                clearDataGrid();
                $('#zjk-competitor-dg').datagrid({
                    queryParams:null
                });
                $('#zjk-competitor-dg').datagrid({
                    url : 'showCompetitorList',
                    method : 'post',
                    queryParams : {
                        comName : searchVal
                    }
                });

                $('#zjk-competitor-dg').datagrid('reload');
            }else if (searchName == 'comPower'){
                clearDataGrid();
                $('#zjk-competitor-dg').datagrid({
                    queryParams:null
                });
                $('#zjk-competitor-dg').datagrid({
                    url : 'showCompetitorList',
                    method : 'post',
                    queryParams : {
                        comPower : searchVal
                    }
                });

                $('#zjk-competitor-dg').datagrid('reload');


            }


        }

    });

    //点击工具栏的“查询所有”按钮，查询所有竞争对手
    $("#zjk-competitor-tools-findAll").linkbutton({
        onClick:function () {
            $('#zjk-competitor-dg').datagrid({
                queryParams : null
            });
            $('#zjk-competitor-dg').datagrid({
                url : 'showCompetitorList',
                method : 'post',
                queryParams : {
                    stage : null
                },
            });
            $('#zjk-competitor-dg').datagrid('reload');
            $("#competitor-search").searchbox('setValue',null);
        }
    });


    //打开添加竞争对手对话框
    $("#zjk-add-competitor-btn").linkbutton({onClick:function () {
            $('#zjk-competitor-add-dialog').dialog({
                title:'添加竞争对手'
            });
            $('#zjk-competitor-add-dialog').dialog('open');

        }});
    $("#zjk-competitor-add-dialog").dialog({
        onLoad:function () {
            //获取客户名称
            $("#add-competitor-client-combobox").combobox({
                url:'getClientInfoName',
                method:'post',
                panelHeight:'auto',
                valueField:'cid',
                textField:'cname'
            });
            //获取对应机会
            $("#add-competitor-saleChance-combobox").combobox({
                url:'getSaleChanceName',
                method:'post',
                panelHeight:'auto',
                valueField:'chId',
                textField:'chanceTheme'


            });
        }
    });
    //关闭按钮事件
    $("#zjk-competitor-add-dialog-bb-close").linkbutton({onClick:function () {
            $('#zjk-competitor-add-dialog').window('close');
            //清空表单
            $('#add-zjk-competitor-f').form('clear');
        }});
    //保存按钮事件（保存添加的竞争对手）
    $("#zjk-competitor-add-dialog-bb-save").linkbutton({onClick:function () {
            $('#add-zjk-competitor-f').form({
                onSubmit:function (parmps) {
                    //进行表单验证
                    return $(this).form('validate');
                },
                success:function (r) {
                    r = eval('('+r+')');
                    if (r.success){
                        $.messager.alert("提示","恭喜，添加成功！");
                        $('#zjk-competitor-add-dialog').dialog('close');

                        //刷新展示列表
                        $('#zjk-competitor-dg').datagrid('reload');
                        //清空表单
                        $('#add-zjk-competitor-f').form('clear');

                    }else {
                        $.messager.alert("错误",r.messager);
                    }
                }
            }).submit();
        }});

    //工具栏删除按钮事件
    $("#zjk-competitor-tools-remove").linkbutton({
        onClick: function () {
            var rows = $('#zjk-competitor-dg').datagrid('getChecked');
            if (rows.length == 0) {
                $.messager.alert("提示", "请先选择要删除的行！");
            } else {
                $.messager.confirm("询问", "您确定要删除吗？", function (v) {
                    if (v) {
                        for (var i = 0; i < rows.length; i++) {
                            if (rows[i]) {
                                var id = rows[i].comId;
                                $.post('deleteCompetitor', {comId: id}, function (r) {
                                    if (r.success) {
                                        $('#zjk-competitor-dg').datagrid('reload');
                                    } else {
                                        $.messager.alert("提示", r.messager);
                                    }
                                });
                            }
                        }
                    }
                })
            }
        }
    });
    //操作栏事件处理
    $("#zjk-competitor-dg").datagrid({
        onLoadSuccess:function (data) {
            $("a[name='competitor-details']").linkbutton({
                width:40,iconCls:'icon-my_detail',plain:true
            });
            //操作栏删除事件
            $("a[name='competitor-delete']").linkbutton({
                width: 40, iconCls: 'icon-my_delete', plain: true,
                onClick: function () {
                    $('#zjk-competitor-dg').datagrid({
                        onClickCell: function (index) {
                            var rows = $('#zjk-competitor-dg').datagrid('getRows');
                            var comid = rows[index].comId;
                            $.post('deleteCompetitor', {comId: comid}, function (r) {
                                if (r.success) {
                                    $().messager.alert("成功","删除成功！")
                                    $('#zjk-competitor-dg').datagrid('reload');
                                } else {
                                    $.messager.alert("提示", r.message);
                                }
                            });
                            $('#zjk-competitor-dg').datagrid({
                                onClickCell: function (index) {
                                }
                            });
                        }

                    });


                }
            });
            //修改竞争对手信息
            $("a[name='competitor-edit']").linkbutton({
                width:40,iconCls:'icon-my_edit',plain:true,
                onClick : function() {
                    $("#zjk-competitor-dg").datagrid({
                        onClickCell : function(index) {
                            var rows = $('#zjk-competitor-dg').datagrid('getRows');// 获得所有行
                            var row = rows[index];// 根据index获得其中一行。
                            if (row) {
                                $("#zjk-competitor-add-edit-dialog").window("open").panel({
                                    onLoad : function() {
                                        $("#zjk-competitor-f").form('load', row);

                                        $("#competitor-client-combobox").combobox({
                                            url: 'saling/getClientInfoList',
                                            method: 'post',
                                            valueField: 'cid',
                                            textField: 'cname',
                                            hasDownArrow: false,
                                            panelHeight: 'auto',
                                            editable:false,
                                            readonly:true
                                        });

                                        $("#competitor-saleChance-combobox").combobox({
                                            url:'getSaleChanceName',
                                            method:'post',
                                            panelHeight:'auto',
                                            valueField:'chId',
                                            textField:'chanceTheme',
                                            hasDownArrow: false,
                                            editable:false,
                                            readonly:true

                                        });
                                    }
                                });
                            }
                            $("#zjk-competitor-dg").datagrid({
                                onClickCell : function(index) {
                                }
                            });


                        }
                    });



                }
            });

        }
    });

    //编辑对话框保存按钮事件
    $("#zjk-competitor-add-edit-dialog-bb-save").linkbutton({
        onClick:function () {
            $('#zjk-competitor-f').form({
                onSubmit:function (parmps) {
                    //进行验证
                    return $(this).form('validate');

                },
                success:function (r) {
                    r = eval('('+r+')');
                    if(r.success){
                        //点击关闭，则窗口关闭
                        $.messager.alert('成功', '恭喜，修改成功！');
                        $('#zjk-competitor-add-edit-dialog').window('close');
                        $('#zjk-competitor-f').form('clear');
                        $('#zjk-competitor-dg').datagrid('reload');
                    }else {
                        $.messager.alert("错误",r.messager);
                    }
                }
            }).submit();

        }
    });

})
//添加操作栏图标
function format(value,row,index){
    var str=/*'<a name="competitor-details" href="#" class="easyui-linkbutton " title="视图"></a>'+*/
        '<a name="competitor-delete" href="#" class="easyui-linkbutton " title="删除"></a>'+
        '<a name="competitor-edit" href="#" class="easyui-linkbutton zjk-edit-competitor-btn" title="编辑"></a>';
    return str;

}

/* 清除数据表格中的数据 */
function clearDataGrid() {
    // 获取当前页的记录数
    var item = $('#zjk-competitor-dg').datagrid('getRows');
    if (item != null) {
        for (var i = item.length - 1; i >= 0; i--) {
            var index = $('#zjk-competitor-dg').datagrid('getRowIndex', item[i]);
            $('#zjk-competitor-dg').datagrid('deleteRow', index);
        }
    }

}
